package seismosurfer.gui.panel;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Properties;

import javax.help.CSH;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import com.bbn.openmap.LightMapHandlerChild;
import com.bbn.openmap.PropertyConsumer;
import com.bbn.openmap.gui.MapPanelChild;
import com.bbn.openmap.gui.OMComponentPanel;
import com.bbn.openmap.gui.OverviewMapHandler;
import com.bbn.openmap.util.PropUtils;

/**
 * This panel contains the OverviewMapHandler and the 
 * SeismoLayersPanel components.
 *
 */
public class ControlPanel extends OMComponentPanel implements MapPanelChild {
    
    private static final long serialVersionUID = 4869688945012161112L;

    LinkedList children = new LinkedList();

    protected JScrollPane scrollPane;

    public ControlPanel() {
        super();
        createPanel();
    }

    protected void createPanel() {

        setLayout(new BorderLayout());

        JPanel navBox = new JPanel();
        navBox.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        navBox.setLayout(new BorderLayout());

        OverviewMapHandler overviewMap = new OverviewMapHandler();
        overviewMap.setUseAsTool(false);
        overviewMap.setPreferredSize(new Dimension(100, 150));
        overviewMap.setBorder(BorderFactory.createRaisedBevelBorder());
        overviewMap.setPropertyPrefix("OverviewMapHandler");

        CSH.setHelpIDString(overviewMap, "overviewMap");

        children.add(overviewMap);

        navBox.add(overviewMap, BorderLayout.CENTER);

        add(navBox, BorderLayout.NORTH);

        SeismoLayersPanel layersPanel = new SeismoLayersPanel();
        layersPanel.setPropertyPrefix("SeismoLayersPanel");
        // layersPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
        children.add(layersPanel);

        // scrollPane = new JScrollPane(layersPanel,
        // ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
        // ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        // scrollPane.setPreferredSize(new Dimension(100, 150));

        add(layersPanel, BorderLayout.CENTER);
        validate();
    }

    public void findAndInit(Object someObj) {
        Iterator it = children.iterator();
        while (it.hasNext()) {
            Object obj = it.next();
            if (obj instanceof LightMapHandlerChild) {
                ((LightMapHandlerChild) obj).findAndInit(someObj);
            }
        }
    }

    public void findAndUndo(Object someObj) {
        Iterator it = children.iterator();
        while (it.hasNext()) {
            Object obj = it.next();
            if (obj instanceof LightMapHandlerChild) {
                ((LightMapHandlerChild) obj).findAndUndo(someObj);
            }
        }
    }

    public void setProperties(String prefix, Properties props) {
        super.setProperties(prefix, props);

        prefix = PropUtils.getScopedPropertyPrefix(prefix);
        String pl = props.getProperty(prefix + PreferredLocationProperty);
        if (pl != null) {
            setPreferredLocation(pl);
        }

        Iterator it = children.iterator();
        while (it.hasNext()) {
            Object obj = it.next();
            if (obj instanceof PropertyConsumer) {
                // Each property prefix will be set with the marker
                // name for the OMControlPanel plus the class name
                // already set as property prefix in the constructor.
                String newPrefix = prefix
                        + ((PropertyConsumer) obj).getPropertyPrefix();
                ((PropertyConsumer) obj).setProperties(newPrefix, props);
            }
        }
    }

    public Properties getProperties(Properties props) {
        props = super.getProperties(props);

        props.put(PropUtils.getScopedPropertyPrefix(this)
                + PreferredLocationProperty, getPreferredLocation());

        Iterator it = children.iterator();
        while (it.hasNext()) {
            Object obj = it.next();
            if (obj instanceof PropertyConsumer) {
                ((PropertyConsumer) obj).getProperties(props);
            }
        }
        return props;
    }

    public Properties getPropertyInfo(Properties props) {
        props = super.getPropertyInfo(props);

        props
                .put(PreferredLocationProperty,
                        "The preferred BorderLayout direction to place this component.");

        Iterator it = children.iterator();
        while (it.hasNext()) {
            Object obj = it.next();
            if (obj instanceof PropertyConsumer) {
                ((PropertyConsumer) obj).getPropertyInfo(props);
            }
        }
        return props;
    }

    /**
     * BorderLayout.WEST by default for this class.
     */
    protected String preferredLocation = java.awt.BorderLayout.WEST;

    /**
     * MapPanelChild method.
     */
    public void setPreferredLocation(String value) {
        preferredLocation = value;
    }

    /** MapPanelChild method. */
    public String getPreferredLocation() {
        return preferredLocation;
    }

}
